import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:leader_help/page/home/key_think_tank/key_think_tank_view.dart';
import 'package:leader_help/page/task/main/task/models/plan.dart';
import 'package:leader_help/utils/dialog_util.dart';

import 'index.dart';

class MakePlanController extends GetxController {
  MakePlanController();

  final state = MakePlanState();

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    state.mod = Get.arguments;
    state.list = [
      PlanModel(
        from: '省财政厅',
        title: '资金资源赋能最具潜力的94家企业，撬动市场资源的高品质配置',
      ),
      PlanModel(
        from: '省工信厅',
        title: '通过技术引导，增加工业企业宣讲活动，吸引市场资源对工业企业的高品质配置。',
      ),
      PlanModel(
        from: '省商务厅',
        title: '通过增强招商引资力度，提高对企业的高品质配置。',
      ),
    ];
    for (var element in state.list!) {
      if (element.title == state.mod?.title) {
        element.isSelected = true;
      }
    }
  }

  void actionCell(PlanModel mod) {
    Get.to(() => const KeyThinkTankPage());
  }

  void actionCellChoose(PlanModel mod) {
    state.list?.forEach((element) {
      if (mod.title == element.title) {
        element.isSelected = !(mod.isSelected == true);
      } else {
        element.isSelected = false;
      }
    });
    update();
  }

  void actionPlan() {
    if (state.list == null) {
      return;
    }
    var _list = state.list!.where((element) => element.isSelected == true);
    if (_list.isEmpty) {
      EasyLoading.showToast('请先选定方案');
      return;
    }
    showAlert(
      msg: '已将任务安排发送给办公厅秘书一处，由办公厅秘书一处进行任务交办并由督查室盯落实，反馈执行进展。',
      cancelText: null,
      sureAction: () {
        Get.back(result: _list.first);
      },
    );
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
